/*
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/24 15:11
 */
package org.mechanic.fund.service.product.impl;

import org.mechanic.core.Constants;
import org.mechanic.fund.domain.product.Product;
import org.mechanic.fund.domain.product.ProductNetvalue;
import org.mechanic.fund.domain.utils.mapper.DtoBeanMapper;
import org.mechanic.fund.repository.product.ProductNetvalueRepository;
import org.mechanic.fund.service.product.IProductNetvalueService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 产品净值服务接口
 *
 * @author mechanic
 * @version 0.0.1
 */
@Service
public class ProductNetvalueService implements IProductNetvalueService {
    private static Logger logger = LoggerFactory.getLogger(ProductTypeService.class);

    private ProductNetvalueRepository netvalueRepository;

    @Autowired
    public ProductNetvalueService(ProductNetvalueRepository netvalueRepository) {
        this.netvalueRepository = netvalueRepository;
    }

    /**
     * 返回每类基准信息最新的一条记录
     *
     * @return 每类基准信息最新的一条记录
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public Page<ProductNetvalue> getProductNetvalueNewestList(PageRequest pageRequest) {
        return this.netvalueRepository.findNewestByProduct(pageRequest);
    }

    /**
     * 返回匹配指定产品编号的按createTime降序排列的产品净值列表
     *
     * @param productId 产品编号
     * @return 产品净值列表
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public Page<ProductNetvalue> getProductNetvalueList(String productId, PageRequest pageRequest) {
        Assert.hasLength(productId, "productId 不能为空");
        Product product = new Product();
        product.setId(productId);

        //PageRequest pageRequest = new PageRequest(0,1);
        return this.netvalueRepository.findByProductOrderByNewestDateDesc(product, pageRequest);
    }

    /**
     * 返回匹配指定主键编号的对象
     *
     * @param id 主键编号
     * @return 对象实体
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public ProductNetvalue getById(String id) {
        return this.netvalueRepository.findOne(id);
    }

    /**
     * 保存。 主键为空则create，不为空则update。
     *
     * @param productNetvalues 被保存的对象
     */
    @Override
    public void save(List<ProductNetvalue> productNetvalues, String operationUserId) {
        this.netvalueRepository.save(DtoBeanMapper.setOperationToList(productNetvalues, operationUserId));
    }

    /**
     * 删除匹配主键ID的对象
     *
     * @param id 主键ID
     */
    @Override
    public void delete(String id) {
        try {
            this.netvalueRepository.delete(id);
        } catch (EmptyResultDataAccessException e) {
            logger.info(id + "不存在");
        }
    }
}
